package day06_exception_collection.com.itheima.d5_list;

import org.w3c.dom.Node;

import java.util.StringJoiner;

public class MyLinkedList<E> {
    private int size = 0;

    Node<E> first;

    public static class Node<E>{
        E item;
        Node<E> next;

        public Node(E item, Node<E> next){
            this.item = item;
            this.next = next;
        }
    }
    public boolean add(E e){
        Node<E> newNode = new Node<>(e, null);
        if (first == null){
            first = newNode;
        }else{
            Node<E> temp = first;
            while (temp.next != null){
                temp = temp.next;
            }
            temp.next = newNode;
        }
        size++;
        return true;
    }

    @Override
    public String toString() {
        StringJoiner sj = new StringJoiner(",", "[", "]");
        Node<E> temp = first;
        while (temp.next != null){
            sj.add(temp.item + "");
            temp = temp.next;
        }
        return sj.toString();
    }
    public int size(){
        return size;
    }

    public static void main(String[] args) {
        MyLinkedList<String> list = new MyLinkedList<>();
        list.add("java1");
        list.add("java2");
        list.add("java3");
        list.add("java4");
        list.add("java5");
        list.add("java6");
        list.add("java7");
        list.add("java8");
        list.add("java9");
        list.add("java10");
        list.add("java11");
        System.out.println(list);
    }
}
